package com.eclipse.training.swt.helloworld.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import com.eclipse.training.swt.helloworld.ImageRegistry;
import com.eclipse.training.swt.helloworld.Messages;

public class RightComposite extends Composite {

	private ImageRegistry registry = ImageRegistry.getInstance();

	public RightComposite(Composite parent, int style) {
		super(parent, style);

		/*
		 * Set the layout
		 */
		setLayout(new GridLayout());
		setLayoutData(new GridData(GridData.FILL_BOTH));

		/*
		 * Create the table
		 */
		Table table = new Table(this, SWT.SINGLE | SWT.FULL_SELECTION
				| SWT.V_SCROLL | SWT.H_SCROLL);
		table.setHeaderVisible(true);

		/*
		 * Set the table minimum height
		 */
		GridData tableLayoutData = new GridData(GridData.FILL_HORIZONTAL);
		tableLayoutData.heightHint = 200;
		table.setLayoutData(tableLayoutData);

		/*
		 * Create table columns
		 */
		TableColumn column1 = new TableColumn(table, SWT.NONE);
		column1.setText(Messages.getString("RightComposite.0")); //$NON-NLS-1$
		column1.setWidth(200);
		column1.setImage(registry.getImage(ImageRegistry.IMAGE_ELEMENT));

		TableColumn column2 = new TableColumn(table, SWT.NONE);
		column2.setText(Messages.getString("RightComposite.1")); //$NON-NLS-1$
		column2.setImage(registry.getImage(ImageRegistry.IMAGE_ELEMENT));

		/*
		 * Fill the table
		 */
		for (int i = 0; i < 20; i++) {
			TableItem item = new TableItem(table, SWT.NONE);
			item.setText(new String[] { Messages.getString("RightComposite.2") + i, String.valueOf(i) }); //$NON-NLS-1$
			item.setImage(new Image[] {
					registry.getImage(ImageRegistry.IMAGE_ELEMENT), null });
		}

		column2.pack();

		/*
		 * Create the tree
		 */
		Tree tree = new Tree(this, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
		tree.setLayoutData(new GridData(GridData.FILL_BOTH));

		/*
		 * Fill the tree
		 */
		for (int loopIndex1 = 0; loopIndex1 < 5; loopIndex1++) {
			TreeItem item0 = new TreeItem(tree, 0);
			item0.setText(Messages.getString("RightComposite.3") + loopIndex1); //$NON-NLS-1$
			item0.setImage(registry.getImage(ImageRegistry.IMAGE_TREE));

			for (int loopIndex2 = 0; loopIndex2 < 5; loopIndex2++) {
				TreeItem item1 = new TreeItem(item0, 0);
				item1.setText(Messages.getString("RightComposite.4") + loopIndex2); //$NON-NLS-1$
				item1.setImage(registry.getImage(ImageRegistry.IMAGE_TREE));

				for (int loopIndex3 = 0; loopIndex3 < 5; loopIndex3++) {
					TreeItem item2 = new TreeItem(item1, 0);
					item2.setText(Messages.getString("RightComposite.5") + loopIndex3); //$NON-NLS-1$
					item2.setImage(registry
							.getImage(ImageRegistry.IMAGE_ELEMENT));
				}
			}
			item0.setExpanded(true);
		}

		pack();
	}

}
